import subprocess

def run_cmd(cmd):
    print(f"Running: {cmd}")
    result = subprocess.run(cmd, shell=True)
    if result.returncode != 0:
        raise Exception(f"Command failed: {cmd}")

if __name__ == "__main__":
    print("creating e2e datasets...")
    path = "data/e2e"
    print("train...")
    run_cmd(f"python src/format_converting_e2e.py {path}/train.txt {path}/train_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/train_formatted.jsonl --output {path}/train.jsonl --add_bos --add_eos")

    print("test...")
    run_cmd(f"python src/format_converting_e2e.py {path}/test.txt {path}/test_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/test_formatted.jsonl --output {path}/test.jsonl --add_bos --add_eos")

    print("valid...")
    run_cmd(f"python src/format_converting_e2e.py {path}/valid.txt {path}/valid_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/valid_formatted.jsonl --output {path}/valid.jsonl --add_bos --add_eos")

    print("creating webnlg datasets...")
    path = "data/webnlg_challenge_2017"
    print("train...")
    run_cmd(f"python src/format_converting_webnlg.py {path}/train.json {path}/train_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/train_formatted.jsonl --output {path}/train.jsonl --add_bos --add_eos")

    print("test...")
    run_cmd(f"python src/format_converting_webnlg.py {path}/test.json {path}/test_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/test_formatted.jsonl --output {path}/test.jsonl --add_bos --add_eos")

    print("valid...")
    run_cmd(f"python src/format_converting_webnlg.py {path}/dev.json {path}/valid_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/valid_formatted.jsonl --output {path}/valid.jsonl --add_bos --add_eos")

    print("creating dart datasets...")
    path = "data/dart"
    print("train...")
    run_cmd("python src/format_converting_dart.py data/dart/dart-v1.1.1-full-train.json data/dart/train_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/train_formatted.jsonl --output {path}/train.jsonl --add_bos --add_eos")

    print("test...")
    run_cmd("python src/format_converting_dart.py data/dart/dart-v1.1.1-full-test.json data/dart/test_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/test_formatted.jsonl --output {path}/test.jsonl --add_bos --add_eos")

    print("valid...")
    run_cmd("python src/format_converting_dart.py data/dart/dart-v1.1.1-full-dev.json data/dart/valid_formatted.jsonl")
    run_cmd(f"python src/gpt2_encode.py --vocab vocab --input {path}/valid_formatted.jsonl --output {path}/valid.jsonl --add_bos --add_eos")

    print("script complete!")
